package com.activenetwork.smlite.testcase.events.eventListSection;

import static org.testng.AssertJUnit.assertEquals;

import java.util.ArrayList;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.schema.model.event.EventType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventDetailView.EventSourceSubview;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.grid.DataGrid;
import com.activenetwork.util.grid.SortableGrid.ColumnSort;
import com.activenetwork.util.grid.gson.GridFactory;

public class EventListSection004 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(EventListSection004.class);
	private List<EventType> testEvents = new ArrayList<EventType>(3);
	private DataGrid grid;

	@BeforeClass(alwaysRun = true)
	public void dataSetup() throws DatatypeConfigurationException {
		EventBusiness eventBusiness = new EventBusiness();
		EventBasicType.Location sharedLocation = createSharedLocation();
		EventBasicType.Length sharedLength = createSharedLength();

		EventType event = new EventType();
		EventBasicType eventBasic = new EventBasicType();
		eventBasic.setTitle("Sorting Test on Event Name - A");
		eventBasic.setLocation(sharedLocation);
		eventBasic.setLength(sharedLength);
		event.setBasic(eventBasic);
		eventBusiness.addEvent(event);
		testEvents.add(event);

		loadHomeView();
		event = new EventType();
		eventBasic = new EventBasicType();
		eventBasic.setTitle("Sorting Test on Event Name - B");
		eventBasic.setLocation(sharedLocation);
		eventBasic.setLength(sharedLength);
		event.setBasic(eventBasic);
		eventBusiness.addEvent(event);
		testEvents.add(event);

		loadHomeView();
		event = new EventType();
		eventBasic = new EventBasicType();
		eventBasic.setTitle("Sorting Test on Event Name - C");
		eventBasic.setLocation(sharedLocation);
		eventBasic.setLength(sharedLength);
		event.setBasic(eventBasic);
		eventBusiness.addEvent(event);
		testEvents.add(event);
	}
	
	private EventBasicType.Location createSharedLocation() {
		EventBasicType.Location location = new EventBasicType.Location();
		location.setLocationName("smlite event location 001");
		location.setAddress("address line 1");
		location.setAddressContinue("address line 2");
		location.setCityRegion("Shanghai");
		location.setStateProvince("SH");
		location.setPostalCode("200032");
		location.setCounty("AG");
		location.setPrivate(true);
		return location;
	}

	private EventBasicType.Length createSharedLength()
			throws DatatypeConfigurationException {
		EventBasicType.Length length = new EventBasicType.Length();
		EventBasicType.Length.MultiDay multi = new EventBasicType.Length.MultiDay();
		XMLGregorianCalendar start_calendar = DatatypeFactory.newInstance()
				.newXMLGregorianCalendar();
		start_calendar.setYear(2013);
		start_calendar.setMonth(1);
		start_calendar.setDay(1);
		multi.setStartDate(start_calendar);
		XMLGregorianCalendar end_calendar = DatatypeFactory.newInstance()
				.newXMLGregorianCalendar();
		end_calendar.setYear(2013);
		end_calendar.setMonth(1);
		end_calendar.setDay(5);
		multi.setEndDate(end_calendar);
		length.setMultiDay(multi);
		return length;
	}

	private void loadHomeView() {
		PageLoader.load(EventSourceSubview.class).events();
	}

	@Test
	public void Verify_Sorting_by_Event_Name_works_correctly() {
		loginBusiness.login();
		
		grid = GridFactory.getGrid("EventsHomeView", "SearchEventGrid");
		grid.sort(2, ColumnSort.ASC);
		checkAsc();
		grid.sort(2, ColumnSort.DESC);
		checkDesc();
		clearTestEvents();
		loginBusiness.logout();
	}
	
	private void checkAsc() {
		String[] titles = fetchTitles();
		assertEquals("Sorting Test on Event Name - A", titles[0]);
		assertEquals("Sorting Test on Event Name - B", titles[1]);
		assertEquals("Sorting Test on Event Name - C", titles[2]);
	}

	private void checkDesc() {
		String[] titles = fetchTitles();
		assertEquals("Sorting Test on Event Name - C", titles[0]);
		assertEquals("Sorting Test on Event Name - B", titles[1]);
		assertEquals("Sorting Test on Event Name - A", titles[2]);
	}

	private String[] fetchTitles() {
		return new String[] { grid.getTd(1, 2).getText(),
				grid.getTd(2, 2).getText(), grid.getTd(3, 2).getText() };
	}

	private void clearTestEvents() {
		EventBusiness eventBusiness = new EventBusiness();
		for (EventType event : testEvents) {
			eventBusiness.deleteEvent(event.getBasic().getTitle(), event);
		}
	}

}
